package com.lechuang.user.manager.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.lechuang.user.dto.SaveUserConsigneeDto;
import com.lechuang.user.dto.UserConsigneeRequestDto;
import com.lechuang.user.entity.UserConsignee;
import com.lechuang.user.manager.UserConsigneeManager;
import com.lechuang.user.service.UserConsigneeService;
import com.lechuang.user.vo.UserConsigneeVo;
import jakarta.annotation.Resource;
import jodd.util.CollectionUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author apple
 * @version 1.0
 * @date 2024/6/24
 */
@Service
@Log4j2
public class UserConsigneeManagerImpl implements UserConsigneeManager {

    @Resource
    private UserConsigneeService userConsigneeService;

    @Override
    public void saveOrUpdate(SaveUserConsigneeDto saveUserConsigneeDto) {
        Integer  defalutFlag = saveUserConsigneeDto.getDefaultFlag();
        if (SaveUserConsigneeDto.DEFAULT_ADDR.equals(saveUserConsigneeDto.getDefaultFlag())){
            List<UserConsignee> userConsigneeList = userConsigneeService.queryByUserIdAndDefaultFlag(saveUserConsigneeDto.getUserId(),saveUserConsigneeDto.getDefaultFlag());
            if (CollectionUtils.isNotEmpty(userConsigneeList)){
                List<Long> idList = userConsigneeList.stream().map(x -> x.getId()).collect(Collectors.toList());
                userConsigneeService.updateDefaultFlagByIds(idList,defalutFlag);
            }
        }
        UserConsignee userConsignee =new UserConsignee()
                .setUserId(saveUserConsigneeDto.getUserId())
                .setConsigneeName(saveUserConsigneeDto.getConsigneeName())
                .setConsigneePhone(saveUserConsigneeDto.getConsigneePhone())
                .setConsigneeAddressDetail(saveUserConsigneeDto.getConsigneeAddressDetail())
                .setProvinceId(saveUserConsigneeDto.getProvinceId())
                .setProvinceName(saveUserConsigneeDto.getProvinceName())
                .setCityId(saveUserConsigneeDto.getCityId())
                .setCityName(saveUserConsigneeDto.getCityName())
                .setAreaId(saveUserConsigneeDto.getAreaId())
                .setAreaName(saveUserConsigneeDto.getAreaName())
                .setDefaultFlag(saveUserConsigneeDto.getDefaultFlag());
        userConsignee.setId(saveUserConsigneeDto.getId());
        if (Objects.isNull(userConsignee.getId())){
            userConsigneeService.save(userConsignee);
        }else {
            userConsigneeService.updateById(userConsignee);
        }
    }

    @Override
    public List<UserConsigneeVo> getConsigneeList(UserConsigneeRequestDto userConsigneeRequestDto) {
        Long userId = userConsigneeRequestDto.getUserId();
        List<UserConsigneeVo> userConsigneeVos = new ArrayList<>();
        List<UserConsignee> userConsigneeList = userConsigneeService.queryByUserId(userId);
        userConsigneeList.forEach(bean->{
            UserConsigneeVo userConsigneeVo =new UserConsigneeVo();
            BeanUtil.copyProperties(bean,userConsigneeVo);
            userConsigneeVos.add(userConsigneeVo);
        });
        return userConsigneeVos;
    }

    @Override
    public void removeById(Long id) {
        userConsigneeService.removeById(id);
    }
}
